#include<bits/stdc++.h>

using namespace std;


class Solution1{
public:
    int trans(int a, int b){
        return b < 0 ? -1 * abs(a) : abs(a);
    }
};
class Solution2{
public:
    void diffSum(int n, int m){
        if(n == 0){
            //...
        }
        vector<int> num1(n), num2(m);
        int count = 2;//表示现在是几进制
        int minOne = min(n, m);//一共要进行几次相加
        int carry = 0;//进位值
        //获取数字
        for (int i = 0; i < n; i++){
            cin >> num1[i];
        }
        for (int i = 0; i < m; i++){
            cin >> num2[i];
        }
        vector<int> ret;
        int index1 = num1.size() - 1, index2 = num2.size() - 1;
        while(index1 >= 0 && index2 >= 0){
            int sum = num1[index1--] + num2[index2--] + carry;
            ret.push_back(sum % count);
            carry = sum >= count ? 1 : 0;
            count++;
        }
        while(index1 >= 0 && carry != 0){
            int sum = num1[index1--] + carry;
            ret.push_back(sum % count);
            carry = sum >= count ? 1 : 0;
            count++;
        }
        while (index2 >= 0 && carry != 0)
        {
            int sum = num1[index2--] + carry;
            ret.push_back(sum % count);
            carry = sum >= count ? 1 : 0;
            count++;
        }
        reverse(ret.begin(), ret.end());
        for(auto val : ret){
            cout << val << " " << endl;
        }
    }
};
class Solution3{
public:
    int dfs(int n, int m, vector<int>num, int count, int minNum, int maxNum){
        if(count == m){
            return minNum;
        }
        for (int i = 0; i < n; i++){
            num[i] = minNum;
        }
    }
    void minList(int n, int m, vector<int>num){
        //dp[i]: 当前到序列长度为i的最小差值
        //状态方程: dp[i] = 

    }
};
int main()
{
    Solution2().diffSum(5, 4);
    return 0;
}